package com.mldong.validation;

import cn.hutool.core.util.ReflectUtil;
import com.mldong.holder.RequestParamIdHolder;
import com.mldong.holder.ValidatorGroupHolder;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@Component
public class MldongLocalValidatorFactoryBean extends LocalValidatorFactoryBean {
    @Override
    public void validate(Object target, Errors errors, Object... validationHints) {
        try {
            if(validationHints.length > 0) {
                ValidatorGroupHolder.set((Class<?>) validationHints[0]);
            }
            if(ReflectUtil.hasField(target.getClass(),"id")) {
                Object object = ReflectUtil.getFieldValue(target,"id");
                if(object!=null) {
                    RequestParamIdHolder.set(object.toString());
                }
            }
            super.validate(target, errors, validationHints);
        } catch (Exception e) {

        } finally {
            ValidatorGroupHolder.remove();
            RequestParamIdHolder.remove();
        }
    }
}
